// pages/my_kefu/my_kefu.js

import config from './../../config';
import {
    empty,
    message,
    session,
    getOpenId,
} from './../../utils/helper';
import {
    submitForm,
} from './../../services/my_kefu';
import WxValidate from './../../utils/validate';
import qs from 'qs';

//获取应用实例
const app = getApp();
//实例化本地缓存
const cookie = new session();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        body: '',
        tip: true,
        errorMsg: [],
        disabled: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this;
        this.initValidate();
        //显示当前页面的转发按钮
        wx.showShareMenu({
            withShareTicket: true
        })
    },

    formsubmit: function(e) {
        let params = e.detail.value,
            that = this;
        // 传入表单数据，调用验证方法
        if (this.WxValidate.checkForm(params)) {
            this.setData({
                errorMsg: '',
                tip: true,
                disabled: true
            });
            getOpenId(openid => {
                params.openid = openid;
                submitForm(params, {
                    success: () => {
                        that.setData({
                            body: '',
                        });
                        message('反馈成功', () => {
                            const len = getCurrentPages().length;
                            if (len > 1) {
                                wx.navigateBack({
                                    delta: 1
                                })
                            }
                        });
                    },
                    complete: () => {
                        that.setData({
                            disabled: false
                        });
                    }
                });
            });
        } else {
            let errors = this.WxValidate.validationErrors(),
                error = [];
            error[0] = errors[0];
            this.setData({
                errorMsg: error,
                tip: false,
            });
        }
        return false;
    },

    //初始化验证规则
    initValidate() {
        // 验证字段的规则
        const rules = {
            body: {
                required: true,
                minlength: 10,
                maxlength: 300
            },
        }
        // 验证字段的提示信息，若不传则调用默认的信息
        const messages = {
            body: {
                required: '请输入反馈内容',
                minlength: '反馈内容至少10个字',
                maxlength: '反馈内容最多300个字'
            },
        }
        // 创建实例对象
        this.WxValidate = new WxValidate(rules, messages)
    },

    /**
     * 实时显示textarea的字数
     */
    inputs: function(e) {
        // 获取输入框的内容
        var value = e.detail.value;
        // 获取输入框内容的长度
        var len = parseInt(value.length);
        //最多字数限制
        if (len > this.data.max) return;
        // 当输入框内容的长度大于最大长度限制（max)时，终止setData()的执行
        this.setData({
            currentWordNumber: len //当前字数  
        });
    },

    //下拉刷新事件
    onPullDownRefresh() {
        this.onLoad();
        wx.stopPullDownRefresh();
    }

})